-- 呼吸写的,冰汽爬

-- only 呼吸
-- 这么写多简单, 之前非得一个个测试
local SHOW_RANGE_TIME = 30
local C = GLOBAL.rangeTable.click_range
local H = GLOBAL.rangeTable.hover_range
local P = GLOBAL.rangeTable.placer_range


-- click 画圆
local controller = GLOBAL.require "components/playercontroller"
local _OnLeftClick = controller.OnLeftClick
function controller:OnLeftClick(down, ...)
	if InGame() and not down then
		local item = GLOBAL.TheInput:GetWorldEntityUnderMouse()
		if item and item:IsValid() then
			local prefab = item.prefab
			local info = C[prefab]
			if info then
				local color = info[1] or "white"
				local range = info[2]
				if type(range) == "number" then
					local circle = GLOBAL.SpawnPrefab("hrange"):setColor(color):setParentScale(item, range)
					circle:DoTaskInTime(SHOW_RANGE_TIME, circle.Remove)
				end
			end
			-- 妈的，一个tag还藏着掖着
			if prefab == "spiderden" and item:HasTag("bedazzled") and GLOBAL.TUNING then
				local radius = GLOBAL.TUNING.BEDAZZLEMENT_RADIUS
				local range
				if radius then
					if SearchForAnim({"cocoon_medium"}, item) then
						range = radius[2]
					elseif SearchForAnim({"cocoon_small"}, item) then
						range = radius[1]
					elseif SearchForAnim({"cocoon_large"}, item) then
						range = radius[3]
					end
				end
				if range then
					local circle = GLOBAL.SpawnPrefab("hrange"):setColor("grass"):setParentScale(item, range)
					circle:DoTaskInTime(SHOW_RANGE_TIME, circle.Remove)
				end
			end
		end
	end
	return _OnLeftClick(self,down,...)
end


-- hover 画圆
local circle
local function removeCircle()
	if circle and circle:IsValid() then
		circle:Remove()
		circle = nil
	end
end
AddClassPostConstruct("widgets/hoverer",function(hoverer)
	local _Hide = hoverer.text.Hide
	local _SetString = hoverer.text.SetString
	hoverer.text.SetString = function(...)
		local target = GLOBAL.TheInput:GetHUDEntityUnderMouse()
		local prefab = target and target.widget and target.widget.parent and target.widget.parent.item and target.widget.parent.item.prefab
		local bind = prefab and GLOBAL.ThePlayer or GLOBAL.TheInput:GetWorldEntityUnderMouse()
		prefab = prefab or (bind and bind.prefab)
		if prefab and bind and not circle and type(H[prefab]) == "number" then
			circle = GLOBAL.SpawnPrefab("hrange"):setStandardParentScale(bind, H[prefab]):setColor("blue")
		end
		return _SetString(...)
	end
	
	hoverer.text.Hide = function(...)
		removeCircle()
		return _Hide(...)
	end
end)

-- placer画圆
-- 该功能使用时需要关闭几何
for place,info in pairs(P)do
	local color = info[1] or "green"
	local range = info[2]

	if range then
		AddPrefabPostInit(place, function(inst)
			inst:DoTaskInTime(0.1, function()
				GLOBAL.SpawnPrefab("hrange"):setParentScale(inst, range):setColor(color)
			end)
		end)
	end
end

-- -- placer画圆,顺便改变颜色
-- AddComponentPostInit("placer", function(self)
-- 	local _oncanbuild = self.oncanbuild
-- 	local _oncannotbuild = self.oncannotbuild

-- 	self.oncanbuild = function(inst, ...)
-- 		return _oncanbuild and _oncanbuild(inst, ...)
-- 	end

-- 	self.oncannotbuild = function(inst, ...)
-- 		return _oncannotbuild and _oncannotbuild(inst, ...)
-- 	end
-- end)